package com.zelin.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.zelin.shop.entity.SpecificationOptionEntity;
import com.zelin.shop.entity.TypeTemplateEntity;
import com.zelin.shop.service.SpecificationOptionService;
import com.zelin.shop.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.shop.dao.SpecificationDao;
import com.zelin.shop.entity.SpecificationEntity;
import com.zelin.shop.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {

    @Autowired
    private TypeTemplateService templateService;
    @Autowired
    private SpecificationOptionService optionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 功能描述:查询所有的规格及规格选项
     * 返回值:
     * 作者:LZB
     * 时间: 2021/11/5 10:13
     * @param newId
     */
    @Override
    public List<Map> findSpecList(Long newId) {
        // 根据模板id得到模板对象
        TypeTemplateEntity typeTemplateEntity = templateService.getById(newId);
        // 根据模板对象得到规格字符串
        String specIds = typeTemplateEntity.getSpecIds();
        // 转换为List<Map>对象
        List<Map> maps = JSON.parseArray(specIds, Map.class);
        // 遍历集合将规格选项组合进去
        for (Map map : maps){
            // 得到规格id
            long specId = Long.parseLong(map.get("id") + "");
            List<SpecificationOptionEntity> options = optionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", specId));
            // 添加到map集合
            map.put("options",options);
        }
        return maps;
    }

}